const { login } = require('../controller/user');
const { SuccessModule, ErrorModule } = require('../module/resModule');
const getCookieExpires = () => {
    const d = new Date();
    d.setTime(d.getTime() + (24 * 60 * 60 * 1000));
    return d.toGMTString();
}
const handleUserRouter = (req, res) => {
    const method = req.method;
    if(method === 'POST' && req.path === '/api/user/login') {
        const { username, password } = JSON.parse(req.body);
        const result = login(username, password);
        return result.then(result => {
            if(result.username) {
                res.setHeader('Set-Cookie', `username=${result.username};path=/;httpOnly;expires=${getCookieExpires()}`);
                return new SuccessModule('登陆成功');
            } else {
                return new ErrorModule('账号或密码错误');
            }
        })
    }

    //  登录验证

}
module.exports = handleUserRouter;